rm(list=ls(all=T))
library(reshape)
library(ggplot2)

# holdouts = c('H1', 'H2', 'H3', 'H4', 'H5')
# allele = 'A0202'
# 
# factors = read.csv(sprintf('/Users/lars/algo/project/%s/factor_matrix', allele), sep = " ", header=F, row.names = 1)
# factors = cbind(factors,NA,NA)
# for (i in 1:5){
#   factors[,i] = as.factor(factors[,i])
# }
# colnames(factors) = c('holdout','fold','neurons','encoding','seed','correlation','test.cor')
# 
# ensemble = as.data.frame(matrix(nrow=3,ncol=3))
# colnames(ensemble) = c('ensemble','ensemble_sp','ensemble_bl')
# 
# fold = 1
# for (nam in holdouts){
#   ens_bl = read.csv(sprintf('/Users/lars/algo/project/%s/%s.ensamble_eval_pred_bl', allele, nam), sep = " ", header=F)
#   ens_sp = read.csv(sprintf('/Users/lars/algo/project/%s/%s.ensamble_eval_pred_sp', allele, nam), sep = " ", header=F)
#   
#   ensemble[fold,1] = cor((ens_bl[,1] + ens_sp[,1])/2, ens_bl[,3])
#   ensemble[fold,2] = cor(ens_sp[,1], ens_sp[,3])
#   ensemble[fold,3] = cor(ens_bl[,1], ens_bl[,3])
#   rm(list=c('ens_bl','ens_sp'))
#   
#   names = read.csv(sprintf('/Users/lars/algo/project/%s/%s.eval.names.txt', allele, nam), sep = "", header=F, colClasses = "character")
#   evals = read.csv(sprintf('/Users/lars/algo/project/%s/%s.singleEvals', allele, nam), sep = " ", header=F, row.names = 1)
#   
#   target = rep(c(F,T),times=ncol(evals)/2)
#   pred = !target
#   
#   cors = as.data.frame(integer(nrow(evals)))
#   rownames(cors) = names[,1]
#   for (i in 1:nrow(evals)){
#     cors[i,] = cor(t(evals[i,target]),t(evals[i,pred]))
#   }
#   factors[names[,1],'correlation'] = cors[names[,1],1]
#   
#   test.cor = read.csv(sprintf('/Users/lars/algo/project/%s/%s.testpredcorr', allele, nam), sep = "", header=F, row.names = 1)
#   factors[names[,1],'test.cor'] = test.cor[names[,1],1]
#   fold = fold+1
# }





# te = ggplot(aes(x=1:nrow(factors), y=correlation), data = factors) + xlab('Cluster #')
# te + geom_point()
# te + geom_point(aes(colour=holdout)) + scale_fill_discrete(name="# Neurons")
# te + geom_point(aes(colour=fold)) + scale_fill_discrete(name="# Neurons")
# te + geom_point(aes(colour=neurons)) + scale_fill_discrete(name="# Neurons")
# te + geom_point(aes(colour=encoding)) + scale_fill_discrete(name="# Neurons")
#
# te = ggplot(aes(x=1:nrow(factors), y=correlation), data = factors) + xlab('Cluster #')
# ggplot(aes(x=neurons, y=correlation), data = factors) + geom_boxplot() + geom_jitter() + facet_grid(holdout~.)
# ggplot(aes(x=holdout, y=correlation, factor = factor(neurons)), data = factors) + geom_boxplot()




# save(list=c('ensemble','factors'),file=sprintf('/Users/lars/algo/project/%s/wisdom.RData',allele))
allele = 'A0202'
load(sprintf('/Users/lars/algo/project/%s/wisdom.RData',allele))
cond = read.table('/Users/lars/algo/condensed/condensed.testcorr')
colnames(cond) = c('tmp','cor')

ggplot(aes(x=1:nrow(factors), y=correlation), data = factors) + xlab('Cluster #') + geom_point()

ggplot(aes(x=holdout, y=correlation, fill = factor(neurons)), data = factors) + 
  geom_boxplot() + 
  theme(legend.position="top") +
  xlab('Holdout') + 
  ylab('Evaluation set correlation') + 
  scale_fill_manual(values=c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E"), 
                    name="Hidden Neurons") +
  theme(legend.position="top")
ggsave(filename='/Users/lars/algo/aib-nn/report/Graphics/neurons_evalcor.pdf',width=10,height=10,units='cm')

ggplot(aes(x=holdout, y=test.cor, fill = factor(neurons)), data = factors) + 
  geom_boxplot() + 
  theme(legend.position="top") + 
  xlab('Holdout') + 
  ylab('Test set correlation') + 
  scale_fill_manual(values=c("#1B9E77", "#D95F02", "#7570B3", "#E7298A", "#66A61E"), 
                    name="Hidden Neurons") +
  theme(legend.position="top")
ggsave(filename='/Users/lars/algo/aib-nn/report/Graphics/neurons_testcor.pdf',width=10,height=10,units='cm')

ggplot(aes(x=holdout, y=correlation, fill = factor(encoding)), data = factors) + 
  geom_boxplot() +
  xlab('Holdout') + 
  ylab('Evaluation set correlation') + 
  scale_fill_manual(values=c("#AF8DC3", "#7FBF7B"), 
                    name="Encoding",
                    breaks=c("bl", "sp"),
                    labels=c("BLOSUM", "Sparse")) +
  theme(legend.position="top")
ggsave(filename='/Users/lars/algo/aib-nn/report/Graphics/encoding_evalcor.pdf',width=10,height=10,units='cm')

ggplot(aes(x=holdout, y=test.cor, fill = factor(encoding)), data = factors) + 
  geom_boxplot() +
  xlab('Holdout') + 
  ylab('Test set correlation') + 
  scale_fill_manual(values=c("#AF8DC3", "#7FBF7B"), 
                    name="Encoding",
                    breaks=c("bl", "sp"),
                    labels=c("BLOSUM", "Sparse")) +
  theme(legend.position="top")
ggsave(filename='/Users/lars/algo/aib-nn/report/Graphics/encoding_testcor.pdf',width=10,height=10,units='cm')

#Add correlation on figures
cors = as.data.frame(integer(5))
for (i in 1:5){
  ind = factors[,'holdout'] == i
  cors[i,1] = round(cor(factors[ind,'correlation'],factors[ind,'test.cor']),digits=2)
}

cors = cbind(cors, as.factor(1:5))
colnames(cors) = c('cors','holdout')

ggplot(aes(y=test.cor, x=correlation, colour = fold),data=factors) + 
  geom_point() + 
  facet_grid(. ~ holdout) +
  coord_equal() + 
  ylab('Test set correlation') + 
  xlab('Evaluation set correlation') +
  scale_color_discrete(name = 'Subset') + 
  geom_text(data = cors, aes(x=Inf,y=Inf, label = paste("rho == ", cors, sep = '')), parse = T, hjust = 1.2, vjust = 1.5, colour = 'black', size = 4) +
  theme(legend.position = 'top') +
  guides(colour = guide_legend(title.position = "top", title.hjust = 0.5, label.position = 'top', label.hjust = 0.5))
ggsave(filename='/Users/lars/algo/aib-nn/report/Graphics/test_eval_cor_poster.png',width=20,height=15,units='cm')

ggplot(aes(x=fold, y=correlation), data=factors) + geom_point() +  facet_grid(holdout ~.)

easypred = c(0.79220,0.76214,0.79925,0.82227,0.80013)


ens = data.frame(holdout = factor(1:5), cor = ensemble[,1], y = c(8.5,13,3.5,5,6))
con = data.frame(holdout = factor(1:5), cor = cond[,2], y = c(15,17.5,28.5,10,15.5))
easy = data.frame(holdout = factor(1:5), cor = easypred, y = c(10.5,17,27.5,9.5,12.5))

#gennemsnitlig afstand mellem vores ensemble og vores kondenserede netværk
sum(abs(ens[,'cor']-con[,'cor']))/5

#gennemsnitlig afstand mellem vores ensemble og easypred
sum(abs(ens[,'cor']-easy[,'cor']))/5

#gennemsnitlig afstand mellem vores kondenserede netværk og easypred
sum(abs(con[,'cor']-easy[,'cor']))/5
sum(con[,'cor']-easy[,'cor'])/5

ggplot(aes(x=correlation),data = factors) + 
  geom_density() + 
  geom_point(aes(x=cor, y=y, size = 1, colour = 'red'), data=ens) + 
  geom_point(aes(x=cor, y=y, size = 1, colour = 'blue'), data=con) +
  geom_point(aes(x=cor, y=y, size = 1, colour = 'green'), data=easy) +
  facet_grid(holdout ~.) + 
  xlab('Correlation') +
  ylab('Density') + 
  theme(legend.position="right") +
  scale_size_continuous(guide = FALSE) +
  scale_color_manual(values=c("#E41A1C", "#377EB8", "#4DAF4A"), 
                     name='',
                     breaks=c("red", "blue", "green"),
                     labels=c("Ensemble", "Condensed", "EasyPred")) +
  theme(legend.position = 'top')
ggsave(filename='/Users/lars/algo/aib-nn/report/Graphics/correlation_density.pdf',width=10,height=15,units='cm')